<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" style="text-align: left">
      <el-form-item>
        <el-button style="color: green" icon="el-icon-plus" @click="add"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="departName" label="部门名称" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status == true" style="color: green"
              >正常</span
            >
            <span v-if="scope.row.status == false" style="color: red"
              >停用</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="creationtime" label="创建时间" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" style="color: orange" @click="upd(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" style="color: green" @click="add"
              >添加</el-button
            >
          </template>

          <!-- <el-button type="text" style="color: green" @click="del"
            >删除</el-button
          > -->
        </el-table-column>
      </el-table>
    </template>

    <el-dialog title="编辑部门" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item label="部门类型" prop="departTypeId">
          <el-select
            v-model="form.departTypeId"
            placeholder="请选择部门类型"
            style="width: 419px"
          >
            <el-option label="公司" value="1"></el-option>
            <el-option label="部门" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上级部门" prop="pId">
          <el-select
            v-model="form.pId"
            placeholder="请选择上级部门"
            style="width: 419px"
          >
            <el-option label="广州分公司" value="1"></el-option>
            <el-option label="东莞分公司" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="departName">
          <el-input v-model="form.departName"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="person">
          <el-input v-model="form.person"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="部门状态" prop="status" style="width: 43%">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">正常</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="创建时间" prop="creationtime">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.creationtime"
              style="width: 419px"
            ></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun" style="color: green">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  GetShowdepartManage,
  deletedepartManage,
  upddepartManage,
} from "@/api/sysManage/DepartManage";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      tableData: [],
      dialogFormVisible: false,
      multipleSelection: [],
      ids: {},
      form: {
        id: "",
        departTypeId: "",
        pId: "",
        departName: "",
        person: "",
        phone: "",
        status: "",
        creationtime: "",
      },
    };
  },
  created() {
    this.list();
  },
  methods: {
    //显示
    list() {
      GetShowdepartManage().then((res) => {
        this.tableData = res.data.data;
      });
    },
    //全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }

      this.multipleSelection.forEach((res) => {
        this.ids.id = res.id;
      });
      deletedepartManage(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //修改
    upd(row) {
      this.dialogFormVisible = true;
      this.form = row;
    },
    baocun() {
      upddepartManage(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //跳转
    add() {
      this.$router.push("/SysManage/AddDepartManage");
    },
  },
};
</script>
<style>
.el-table .warning-row {
  background: rgb(230, 253, 253);
}

.el-table .success-row {
  background: #f4ebf9;
}
</style>